package com.zyf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zyf.service.MsmService;
import com.zyf.utils.ConstantPropertiesUtils;
import com.zyf.vo.msm.MsmVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/** 短信服务 业务层实现类
 * @author zyf
 * @date 2021年05月18日 15:06
 */
@Service
public class MsmServiceImpl implements MsmService {


    /**
     * 整合短信服务进行发送
     * @author zyf
     * @date 2021/5/18 15:35
     * @param phone 手机号
     * @param code 验证码
     * @return boolean
     */
    @Override
    public boolean sendCode(String phone, String code) {
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
            return false;
        }
        //整合阿里云短信服务，发送短信
        DefaultProfile profile = DefaultProfile.getProfile(ConstantPropertiesUtils.REGION_ID,
                        ConstantPropertiesUtils.ACCESS_KEY_ID,
                        ConstantPropertiesUtils.SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers",phone);
        //签名名称
        request.putQueryParameter("SignName","乐优商城");
        //短信模板code
        request.putQueryParameter("TemplateCode","SMS_205436421");
        //验证码  使用json格式   {"code":"123456"}
        Map<String,Object> param = new HashMap<>();
        param.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * Mq发送消息
     * @author zyf
     * @date 2021/5/31 17:38
     * @param msmVo 短信实体
     * @return boolean
     */
    @Override
    public boolean sendMq(MsmVo msmVo) {
        if(!StringUtils.isEmpty(msmVo.getPhone())){
            boolean b = this.send(msmVo.getPhone(), msmVo.getParam());
            return b;
        }
        return false;
    }

    private boolean send(String phone, Map<String, Object> paramMap) {
        if (StringUtils.isEmpty(phone)){
            return false;
        }
        //整合阿里云短信服务，发送短信
        DefaultProfile profile = DefaultProfile.getProfile(ConstantPropertiesUtils.REGION_ID,
                ConstantPropertiesUtils.ACCESS_KEY_ID,
                ConstantPropertiesUtils.SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers",phone);
        //签名名称
        request.putQueryParameter("SignName","乐优商城");
        //短信模板code
        request.putQueryParameter("TemplateCode","SMS_205436421");
        //验证码  使用json格式   {"code":"123456"}
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(paramMap));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
